package com.ITArmy.util;

import com.ITArmy.beans.User;
import org.apache.log4j.Logger;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

//获取session中的用户的数据
public class GetUserSession {
    private static org.apache.log4j.Logger logger = Logger.getLogger(GetUserSession.class);

    public User getUser(HttpServletRequest request) {
        HttpSession userSession = request.getSession();
        User user = (User) userSession.getAttribute("user");
        return user;
    }

    //获取session内的用户数据直接放到model里面
    public void addUserModel(HttpServletRequest request, Model model) {
        HttpSession userSession = request.getSession();
        User user = (User) userSession.getAttribute("user");
        if (user != null) {
            logger.info("存在user的session数据...");
            model.addAttribute("user", user);
        } else {
            logger.info("不存在user的session数据...");
        }
    }

    //将用户数据添加到session中
    public void addUserSession(HttpServletRequest request, User user) {
        HttpSession session = request.getSession();
        session.setAttribute("user", user);
    }
    //将主题信息添加到session中
}
